package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Repairrecord;
import com.woniuxy.caradmin.entity.vo.RepairrecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.RepairrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
public class RepairrecordController {
    @Autowired
    private RepairrecordService repairrecordService;

    @GetMapping("/reapairrecordList")
    @ApiOperation("查询所有维修记录")
    public DataGridView selectAllReapairRecord(RepairrecordVo repairrecordVo) {
        Page<Object> page = PageHelper.startPage(repairrecordVo.getPage(), repairrecordVo.getLimit());
        List<Repairrecord> repairrecords = repairrecordService.selectAllReapairRecord(repairrecordVo);
        if(repairrecords!=null && repairrecords.size()>0){
            return  new DataGridView(page.getTotal(),repairrecords);
        } else {
            throw new CarException(ResultCode.REPAIRRECORDISNULL, Constaint.REPAIRRECORDISNULL);
        }
    }

    @GetMapping("/addRepairRecord")
    @ApiOperation("增加维修记录")
    public R addRepairRecord(Repairrecord repairrecord){
        int addRepairRecord = repairrecordService.addRepairRecord(repairrecord);
        if(addRepairRecord!=0){
            return R.ok().code(ResultCode.REPAIRRECORDADDISSUC).message(Constaint.REPAIRRECORDADDISSUC);
        }else {
            throw new CarException(ResultCode.REPAIRRECORDADDISFAI , Constaint.REPAIRRECORDADDISFAI);
        }
    }


    @GetMapping("/updateRepairRecord")
    @ApiOperation("修改维修记录")
    public R updateRepairRecord(Repairrecord repairrecord){
        int updateRepairRecord = repairrecordService.updateRepairRecord(repairrecord);
        if(updateRepairRecord!=0){
            return  R.ok().code(ResultCode.REPAIRRECORDUPDATEISSUC).message(Constaint.REPAIRRECORDUPDATEISSUC);
        }else {
            throw  new CarException(ResultCode.REPAIRRECORDUPDATEISFAI,Constaint.REPAIRRECORDUPDATEISFAI);
        }
    }


    @GetMapping("/deleteRepairRecord")
    @ApiOperation("删除维修记录")
    public R deleteRepairRecord(Integer id){
        int deleteRepairRecord = repairrecordService.deleteRepairRecord(id);
        if(deleteRepairRecord!=0){
            return  R.ok().code(ResultCode.REPAIRRECORDDDELETEISSUC).message(Constaint.REPAIRRECORDDDELETEISSUC);
        }else {
            throw  new CarException(ResultCode.REPAIRRECORDDELETEISFAI,Constaint.REPAIRRECORDDELETEISFAI);
        }

    }

    @GetMapping("/goRepairRecord")
    public ModelAndView goLOgin(ModelAndView modelAndView) {
        modelAndView.setViewName("repairrecord");
        return modelAndView;
    }

}

